package com.apobates.jforum2.troll.threads.mapper;

import com.apobates.jforum2.threads.stub.vo.ThreadsStatsVo;
import com.apobates.jforum2.troll.threads.entity.TopicStats;
import com.apobates.jforum2.troll.utils.core.api.OptionalEntityTransformMapper;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 *
 * @author xiaofanku
 */
public class ThreadsStatsVoMapperEntity implements OptionalEntityTransformMapper<TopicStats, ThreadsStatsVo>{
    @Override
    public Optional<ThreadsStatsVo> transform(Optional<TopicStats> entity) {
        return entity.map(ins->{
            return mapper().apply(ins);
        });
    }

    public static Function<TopicStats, ThreadsStatsVo> mapper(){
        return topicStats -> {
            ThreadsStatsVo tsv = new ThreadsStatsVo();
            //话题的ID
            tsv.setThreads(topicStats.getTopicId());
            //卷的ID
            tsv.setVolumes(topicStats.getVolumesId());
            //版块的ID
            tsv.setBoard(topicStats.getBoardId());
            //回复数
            tsv.setReplies(topicStats.getPostses());
            //查看数
            tsv.setDisplaies(topicStats.getDisplaies());
            //收藏数
            tsv.setFavorites(topicStats.getFavorites());
            //点赞的数
            tsv.setLikes(topicStats.getLikes());
            //最近回复的作者ID
            tsv.setRecentReplyMember(topicStats.getRecentPostsMemberId());
            //最近回复的作者帐号
            tsv.setRecentReplyNickname(topicStats.getRecentPostsMemberNickname());
            //最近回复的日期
            tsv.setRecentReplyDate(topicStats.getRecentPostsDate());
            //统计更新的时间
            tsv.setUpdateDate(topicStats.getUpdateDate());
            //最近回复的回复ID
            tsv.setRecentReply(topicStats.getRecentPostsId());
            //版块的访问连接
            tsv.setBoardLink(produceLink.apply(topicStats.getBoardId(), topicStats.getVolumesId()));
            return tsv;
        };
    }
    
    public static BiFunction<Long, Integer, String> produceLink = (Long boardId, Integer boardgroupId) -> {
        return String.format("/board/%d-%d.xhtml", boardId, boardgroupId);
    };
}